package com.ricchieri.manage.bean;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;

import org.primefaces.component.themeswitcher.ThemeSwitcher;
import org.springframework.security.core.context.SecurityContextHolder;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.common.CustomAuthenticationToken;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "themeSwitchMB")
@SessionScoped
public class ThemeSwitchManageBean implements Serializable {

	private static final long serialVersionUID = -8692713708852275533L;

	private Map<String, String> themes;

	private String theme = BusinessSettings.DEFAULT_THEME;
	private Boolean isFromDatabase = Boolean.FALSE;

	@ManagedProperty(value = "#{UserService}")
	IUserService userService;

	public Map<String, String> getThemes() {
		return themes;
	}

	public String getTheme() {
		
		if (!isFromDatabase && SecurityContextHolder.getContext().getAuthentication() instanceof CustomAuthenticationToken) {
			User currentUser = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication()).getUser();
			if(StringUtils.isNotEmpty(currentUser.getTheme())) {
				theme = currentUser.getTheme();
			}
		}
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	@PostConstruct
	public void init() {
		generateThemeList();
	}

	private void generateThemeList() {
		themes = new TreeMap<String, String>();
		themes.put("Aristo", "aristo");
		themes.put("Black-Tie", "black-tie");
		themes.put("Blitzer", "blitzer");
		themes.put("Bluesky", "bluesky");
		themes.put("Casablanca", "casablanca");
		themes.put("Cupertino", "cupertino");
		themes.put("Dark-Hive", "dark-hive");
		themes.put("Dot-Luv", "dot-luv");
		themes.put("Eggplant", "eggplant");
		themes.put("Excite-Bike", "excite-bike");
		themes.put("Flick", "flick");
		themes.put("Glass-X", "glass-x");
		themes.put("Hot-Sneaks", "hot-sneaks");
		themes.put("Humanity", "humanity");
		themes.put("Le-Frog", "le-frog");
		themes.put("Midnight", "midnight");
		themes.put("Mint-Choc", "mint-choc");
		themes.put("Overcast", "overcast");
		themes.put("Pepper-Grinder", "pepper-grinder");
		themes.put("Redmond", "redmond");
		themes.put("Rocket", "rocket");
		themes.put("Sam", "sam");
		themes.put("Smoothness", "smoothness");
		themes.put("South-Street", "south-street");
		themes.put("Start", "start");
		themes.put("Sunny", "sunny");
		themes.put("Swanky-Purse", "swanky-purse");
		themes.put("Trontastic", "trontastic");
		themes.put("UI-Darkness", "ui-darkness");
		themes.put("UI-Lightness", "ui-lightness");
		themes.put("Vader", "vader");
	}

	public void saveTheme(AjaxBehaviorEvent ajax) {
		setTheme((String) ((ThemeSwitcher) ajax.getSource()).getValue());
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public void reset() {
		User currentUser = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication()).getUser();
		setTheme(currentUser.getTheme());
	}
	
	public void save() {
		User currentUser = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication()).getUser();
		currentUser.setTheme(theme);
		getUserService().updateUser(currentUser);
		FacesUtil.showInfoMessage("themeForm", "changeThemeSuccessful");
	}

	public void setThemes(Map<String, String> themes) {
		this.themes = themes;
	}
}